﻿using System;
using System.Collections.Generic;
using System.Linq;
using Chat_Model;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using Chat_Model.ViewModel;
using Chat_Service;
using NetTaste;

namespace Chat_WebApi.Controllers.v1
{
    /// <summary>
    /// 笔记
    /// </summary>
    [Route("/api/v1/[controller]")]
    [ApiController]
    public class ArticleController : Controller
    {
        private LarArticleClassService _larArticleClassService;
        private LarArticleService _larArticleService;
        private LarArticleAnnexService _larArticleAnnexService;
        private LarArticleDetailService _larArticleDetailService;
        private LarArticleTagsService _larArticleTagsService;
        private LarUsersService _larUsersService;

        /// <summary>
        /// Initializes a new instance of the <see cref="ArticleController"/> class.
        /// 构造函数
        /// </summary>
        /// <param name="larArticleClassService">笔记类型.</param>
        /// <param name="larArticleService">笔记.</param>
        /// <param name="larArticleAnnexService">笔记附件.</param>
        /// <param name="larArticleDetailService">笔记详情.</param>
        /// <param name="larArticleTagsService">笔记标签.</param>
        /// <param name="larUsersService">用户</param>
        public ArticleController(LarArticleClassService larArticleClassService, LarArticleService larArticleService,
            LarArticleAnnexService larArticleAnnexService, LarArticleDetailService larArticleDetailService,
            LarArticleTagsService larArticleTagsService, LarUsersService larUsersService)
        {
            this._larArticleClassService = larArticleClassService;
            this._larArticleService = larArticleService;
            this._larArticleAnnexService = larArticleAnnexService;
            this._larArticleDetailService = larArticleDetailService;
            this._larArticleTagsService = larArticleTagsService;
            this._larUsersService = larUsersService;
        }

        #region 笔记附件

        #endregion

        #region 笔记分类

        [HttpGet("article-class")]
        public Task<MessageModel<object>> ArticleClass()
        {
            int uid = this._larUsersService.GetUserId();
            List<LarArticleClass> larArticleClassesList = this._larArticleClassService.GetList(x => x.UserId == uid);
            if (larArticleClassesList.Any())
                return Task.FromResult(MessageModel<object>.Successed());
            else
                return Task.FromResult(MessageModel<object>.Failed());
        }

        /// <summary>
        /// 笔记本分类列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("classifys")]
        public Task<MessageModel<object>> Classifys()
        {
            int uid = this._larUsersService.GetUserId();
            List<LarArticleClass> larArticleClassesList = this._larArticleClassService.GetList(x => x.UserId == uid);
            if (larArticleClassesList.Any())
                return Task.FromResult(MessageModel<object>.Successed());
            else
                return Task.FromResult(MessageModel<object>.Failed());
            return null;
        }

        /// <summary>
        /// 合并笔记分类
        /// </summary>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        [HttpGet("classify/merge")]
        public Task<MessageModel<object>> ClassifyMerge()
        {
            int uid = this._larUsersService.GetUserId();
            List<LarArticleClass> larArticleClassesList = this._larArticleClassService.GetList(x => x.UserId == uid);
            if (larArticleClassesList.Any())
                return Task.FromResult(MessageModel<object>.Successed());
            else
                return Task.FromResult(MessageModel<object>.Failed());
            return null;
        }

        /// <summary>
        /// 排序笔记分类
        /// </summary>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        [HttpPost("classify/sort")]
        public async Task<MessageModel<object>> ClassifySort([FromBody] ArticleClassifySortParam param)
        {
            int uid = this._larUsersService.GetUserId();
            List<LarArticleClass> larArticleClassList =
                await this._larArticleClassService.GetListAsync(x => x.UserId == uid);
            larArticleClassList = larArticleClassList.OrderBy(x => x.Sort).ToList();
            int index = larArticleClassList.FindIndex(x => x.Id == param.class_id);
            LarArticleClass larArticleClass = larArticleClassList.Find(x => x.Id == param.class_id);
            if (larArticleClass != null) return MessageModel<object>.Failed("排序失败...");
            LarArticleClass larArticleNextClass;
            if (param.sort_type == 1
                && (index + 2) <= larArticleClassList.Count) //下移动
            {
                larArticleNextClass = larArticleClassList[index + 1];
            }
            else if (param.sort_type == 2
                     && index >= 1) // 上移动
            {
                larArticleNextClass = larArticleClassList[index - 1];
            }
            else
            {
                return MessageModel<object>.Failed("排序失败...");
            }

            int tempSort = larArticleClass.Sort;
            larArticleClass.Sort = larArticleNextClass.Sort;
            larArticleNextClass.Sort = tempSort; // 交换排序

            if (await this._larArticleClassService.UpdateAsync(larArticleClass)
                && await this._larArticleClassService.UpdateAsync(larArticleNextClass))
                return MessageModel<object>.Successed("排序完成...");
            else
                return MessageModel<object>.Failed("排序失败...");
        }

        /// <summary>
        /// 删除笔记分类
        /// </summary>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        [HttpPost("classify/delete")]
        public async Task<MessageModel<object>> ClassifyDelete([FromBody] ArticleClassifyBaseParam param)
        {
            if (await this._larArticleClassService.DeleteByIdAsync(param.class_id).ConfigureAwait(false))
                return MessageModel<object>.Successed("笔记分类删除成功...");
            else
                return MessageModel<object>.Failed("笔记分类删除失败...");
        }

        /// <summary>
        /// 添加或修改笔记分类
        /// </summary>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        [HttpPost("classify/editor")]
        public async Task<MessageModel<object>> ClassifyEditor([FromBody] ArticleClassifyEditorParam param)
        {
            int uid = this._larUsersService.GetUserId();
            LarArticleClass larArticleClass = new LarArticleClass();
            if (param.class_id > 0)
            {
                larArticleClass =
                    await this._larArticleClassService.GetSingleAsync(x => x.Id == param.class_id && x.UserId == uid);
                if (larArticleClass == null) return MessageModel<object>.Failed();
                larArticleClass.ClassName = param.class_name;
                if (!await this._larArticleClassService.UpdateAsync(larArticleClass))
                {
                    larArticleClass.Id = 0;
                }
            }
            else
            {
                larArticleClass = new LarArticleClass()
                {
                    ClassName = param.class_name,
                    UserId = uid,
                    CreatedAt = DateTime.Now.ToString()
                };
                larArticleClass.Id = await this._larArticleClassService.InsertReturnIdentityAsync(larArticleClass);
            }

            if (larArticleClass.Id > 0)
            {
                return MessageModel<object>.Successed(new
                {
                    id = larArticleClass.Id,
                });
            }
            else
            {
                return MessageModel<object>.Failed();
            }
        }

        #endregion

        #region 笔记标签

        #endregion

        #region 笔记相关

        /// <summary>
        /// 获取笔记列表接口 
        /// </summary>
        [HttpGet("search")]
        public async Task<MessageModel<object>> Search([FromQuery] string keyword, string find_type, string cid,
            string page)
        {
            return new MessageModel<object>() { };
        }

        #endregion
    }
}